<template>
  <el-dialog customClass="customExcelWidth" :close-on-click-modal="false" :visible.sync="visible" >
    <template slot="title">
      <div class="dialog_title"> 选择导出的Excel文件</div>
    </template>
    <el-radio-group v-model="radio"  type="vertical">
      <div class="radio_span"><el-radio :label="1">项目列表</el-radio></div>
      <div class="radio_span"><el-radio :label="2">进度统计列表</el-radio></div>
    </el-radio-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
      <el-button type="primary" @click="exportExcelHandle()">导出</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import moment from 'moment'
  export default {
    data () {
      return {
        dataForm: {},
        visible: false,
        radio: 1
      }
    },
    methods: {
      init (item) {
        this.visible = true
        this.$nextTick(() => {
          this.dataForm = item
        })
      },
      exportExcelHandle () {
        this.dataListLoading = true
        let startDate = moment(this.dataForm.startDate).format('YYYY-MM-DD')
        let endDate = moment(this.dataForm.endDate).format('YYYY-MM-DD')
        this.$http({
          url: this.$http.adornUrl('/project/manage/exportExcel'),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key,
            'sidx': this.dataForm.sidx,
            'order': this.dataForm.order,
            'startDate': startDate,
            'endDate': endDate,
            'dateItemId': this.dataForm.dateItemId,
            'radio': this.radio
          }),
          responseType: 'blob'
        }).then(({data}) => {
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(data) // 创建下载的链接
          downloadElement.href = href
          downloadElement.download = (this.radio === 1 ? '项目管理列表（' : '进度分析列表（') + startDate + '-' + endDate + '）.xls' // 下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
          this.dataListLoading = false
          this.visible = false
        })
      }
    }
  }
</script>

<style>
  .dialog_title{
    font-weight: 700;
    font-size: 14pt;
  }
  .dialog_title span{
    color: #3b97d7;
  }
  .radio_span {
    padding: 10px;
  }
  .customExcelWidth{
    width:400px;
  }
</style>
